package com.example.oldBookTest.service;


import cn.hutool.json.JSONArray;
import com.example.oldBookTest.utils.BookTraceUtils;
import com.example.oldBookTest.utils.WeBASEUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    WeBASEUtils weBASEUtils;

    public Dict addUser(String address, String addr, String role) {
        List paramList = new ArrayList();

        paramList.add(addr);
        paramList.add(role);

        String reponse = weBASEUtils.commonReqSimple(address
                , "addUser"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj (reponse);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output"));// 非view 函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict registerUser(int type, String addr) {
        List paramList = new ArrayList();

        paramList.add(type);
        paramList.add(addr);

        String reponse = weBASEUtils.commonReqSimple(addr
                , "registerUser"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj (reponse);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output"));// 非view 函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict getUserfotId(String addr, int userId) {
        List paramList = new ArrayList();
        paramList.add(userId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getUserfotId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        JSONArray ids      = (JSONArray)resArr.get(0);
        JSONArray types = (JSONArray)resArr.get(1);
        JSONArray carbonPointes = (JSONArray)resArr.get(2);
        JSONArray donNums = (JSONArray)resArr.get(3);
        JSONArray reqNums = (JSONArray)resArr.get(4);
        JSONArray addresses = (JSONArray)resArr.get(4);

        Dict res = Dict.create();
        List userList = new ArrayList();

        for (int i = 0 ; i < ids.size(); i++ ) {

            Dict item  = new Dict()
                    .set("id", ids.getInt(i))
                    .set("type", types.get(i))
                    .set("carbonPoint", carbonPointes.get(i))
                    .set("donNum", donNums.get(i))
                    .set("reqNum", reqNums.get(i))
                    .set("address", addresses.get(i));
            userList.add(item);

        }

        res.set("data", userList);
        res.set("status", 200);

        return  res;
    }
}
